/**
 * Copyright HCP Technology 
 * www.hcp.vn
 */
package util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import play.Logger;
import play.data.Upload;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;
import com.google.appengine.api.files.FileWriteChannel;
import com.google.appengine.api.files.FinalizationException;
import com.google.appengine.api.files.LockException;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;

public class Util {
	
	public static String ERROR_404 = "errors/404_page.html";
	public static String HOME_FO = "Application/index.html";
	public static String ADMIN_CONTENT = "Admin/content.html";
	
	public static String SUCCESS = "true";
	public static String FAIL = "false";
	public static int PAGE_ITEM = 10; 
	public static String fullURL(String staticUrl, String action,Map<String, Object> args) {
		if(staticUrl != null) {
			return staticUrl + "/" + args.get("page");
		}
		return play.mvc.Router.getFullUrl(action, args);
	}
	
	public static String convertUTF8TOASCII(String title){
		String ascii = java.text.Normalizer.normalize(title, java.text.Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+","");
		return ascii.replaceAll("([\\., '\"])", "-");
	}

	/**
	 * @param imagesService
	 * @param oldImage
	 * @return
	 */
	public static byte[] scaleImage(ImagesService imagesService, Image oldImage) {
		Transform resize = ImagesServiceFactory.makeResize(188, 188);
		Image newImage = imagesService.applyTransform(resize, oldImage);
		return newImage.getImageData();
	}
	
	/**
	 * @param data
	 * @return
	 */
	public static String saveContent(Upload data) {
		FileService fileService = FileServiceFactory.getFileService();
		  AppEngineFile file;
		try {
			file = fileService.createNewBlobFile(data.getContentType(), data.getFileName());
			// Open a channel to write to it
			boolean lock = true;
			FileWriteChannel writeChannel = fileService.openWriteChannel(
					file, lock);
			writeChannel.write(ByteBuffer.wrap(data.asBytes()));
			writeChannel.closeFinally();
			BlobKey key = fileService.getBlobKey(file);
			return key.getKeyString();
			
		} catch (FileNotFoundException e) {
			Logger.info(e.getMessage());
		} catch (FinalizationException e) {
			Logger.info(e.getMessage());
		} catch (LockException e) {
			Logger.info(e.getMessage());
		} catch (IOException e) {
			Logger.info(e.getMessage());
		}
		return null;
	}

	/**
	 * @param image
	 */
	public static void removeBlob(String path) {
		System.out.println(">>>>>>>>>path remove" + path);
		if(path != null) {
			BlobKey key = new BlobKey(path);
			BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();
			blobStoreService.delete(key);
		}
	}
}
